package ciyuan.Item.SkillBook;

import ciyuan.Effect.Effects;
import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.server.ServerWorld;

/**
 * @author 150149
 */
public class LifeStream extends SkillBookType {

    public LifeStream() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 60 * 1000);
        if (playerEntity.level.isClientSide) {
            return;
        }
        BlockPos blockPos = playerEntity.blockPosition();

        SkillUtils.THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<20;i++) {
                    if (playerEntity.level instanceof ServerWorld) {
                        for (double j=blockPos.getX()-3;j<blockPos.getX()+3;j=j+0.1) {
                            for (double k=blockPos.getZ()-3;k<blockPos.getZ()+3;k=k+0.1) {
                                if (blockPos.distSqr(j,blockPos.getY()+1,k,false)>7 && blockPos.distSqr(j,blockPos.getY()+1,k,false)<8 ) {
                                    ((ServerWorld)playerEntity.level).sendParticles(ParticleTypes.DRIPPING_LAVA, j , blockPos.getY()+1, k, 0, 0, 0.0D, 0, 0.0D);
                                }
                            }
                        }
                    }
                    for(LivingEntity livingentity : playerEntity.level.getEntitiesOfClass(LivingEntity.class, new AxisAlignedBB(
                            blockPos.getX()+4,blockPos.getY()+3,blockPos.getZ()+4,blockPos.getX()-4,blockPos.getY()-3,blockPos.getZ()-4
                    ))) {
                        if (livingentity == playerEntity || playerEntity.isAlliedTo(livingentity)) {
                            if (blockPos.distSqr(livingentity.getX(), livingentity.getY(), livingentity.getZ(), false) < 7.0D) {
                                if (playerEntity.level instanceof ServerWorld) {
                                    livingentity.setHealth(livingentity.getHealth()*1.05F);
                                    livingentity.addEffect(new EffectInstance(Effects.life_stream,20,0));
                                }
                            }
                        }
                    }
                    SkillUtils.sleep(1000);
                }
            }
        });
        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    @Override
    public String getSkillCastName() {
        return "LifeStream";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.33");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.33");
    }
}
